unit UntNovinDelphiSample;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls,clsNovinLock, Mask, Menus , Clipbrd,
  Buttons;

type
  TFrmNovinDelphiSample = class(TForm)
    lbVID: TLabel;
    edVID: TEdit;
    lbPassword: TLabel;
    edPassword: TEdit;
    PageControl1: TPageControl;
    TabGeneral: TTabSheet;
    TabBlockData: TTabSheet;
    TabData: TTabSheet;
    TabEncryption: TTabSheet;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    lbChkDeviceReady: TLabel;
    lbPID: TLabel;
    lbSerial: TLabel;
    btnGetPID: TButton;
    btnGetSerial: TButton;
    btnGetDataBlock: TButton;
    btnSetDataBlock: TButton;
    Label4: TLabel;
    lbVersion: TLabel;
    btnGetVersion: TButton;
    Label5: TLabel;
    lbMemorySize: TLabel;
    Button1: TButton;
    Label6: TLabel;
    edAddress: TEdit;
    memDataBlock: TMemo;
    edLength: TEdit;
    lbLength: TLabel;
    Label7: TLabel;
    edHex: TEdit;
    Label8: TLabel;
    edChar: TEdit;
    Label9: TLabel;
    edAddress2: TEdit;
    btnSetByte: TButton;
    btnGetByte: TButton;
    Label10: TLabel;
    edIteration: TEdit;
    Label11: TLabel;
    chkCharView: TCheckBox;
    Label12: TLabel;
    btnEncrypt: TButton;
    btnDecrypt: TButton;
    medString: TMaskEdit;
    lbResult: TLabel;
    PopupMenu1: TPopupMenu;
    mnuCopy: TMenuItem;
    btnCopy: TBitBtn;
    btnGetDeviceReady: TButton;
    cbxDevs: TComboBox;
    lbDev: TLabel;
    procedure btnGetPIDClick(Sender: TObject);
    procedure btnGetSerialClick(Sender: TObject);
    procedure btnGetVersionClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure btnGetDataBlockClick(Sender: TObject);
    procedure btnSetDataBlockClick(Sender: TObject);
    procedure edAddressKeyPress(Sender: TObject; var Key: Char);
    procedure edHexChange(Sender: TObject);
    procedure edAddress2Change(Sender: TObject);
    procedure edCharChange(Sender: TObject);
    procedure btnGetByteClick(Sender: TObject);
    procedure btnSetByteClick(Sender: TObject);
    procedure chkCharViewClick(Sender: TObject);
    procedure medStringChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure medResultChange(Sender: TObject);
    procedure btnEncryptClick(Sender: TObject);
    procedure btnCopyClick(Sender: TObject);
    procedure btnDecryptClick(Sender: TObject);
    procedure btnGetDeviceReadyClick(Sender: TObject);
    procedure cbxDevsChange(Sender: TObject);
  private
    { Private declarations }
    procedure ChangeDeviceEvent(var Msg: TMessage); message WM_DEVICECHANGE;
  public
    { Public declarations }
  end;

var
  FrmNovinDelphiSample: TFrmNovinDelphiSample;
  fStr : WideString ;
  fResult : WideString;
  nlock : TclsHIDLock;
  MsgCDE: TMessage;
  checking: Boolean;
implementation

uses StdConvs;

{$R *.dfm}

Procedure ShowErr();
Begin
    If nlock.ErrNo <> 0 Then
        MessageDlg('Error Number ' + inttostr(nlock.ErrNo)+ Char(13)+nlock.ErrDescr,mtError , [mbOK] ,0);
End;

procedure TFrmNovinDelphiSample.ChangeDeviceEvent(var Msg: TMessage);
begin
  if checking then exit;
  checking := true;
  Sleep(500);
  cbxDevs.Items.Clear;
  nlock.GetFirstDevice;
  while nlock.ErrNo=0 do
  begin
    cbxDevs.Items.Add(nlock.CurrDevSerial);
    if nlock.CurrDevSerial=nlock.SelectedDevSerial then
      cbxDevs.ItemIndex := cbxDevs.Items.Count-1;
    nlock.GetNextDevice;
  end;


  nlock.GetDeviceReady();
  If nlock.ErrNo = 0 Then
  Begin
      lbChkDeviceReady.Caption := 'Ready' ;
      lbChkDeviceReady.Font.Color   := clGreen ;
  end
  Else
  Begin
      lbChkDeviceReady.Caption := 'Not Ready';
      lbChkDeviceReady.Font.Color := clRed;
  End;
  checking := false;
end;

procedure TFrmNovinDelphiSample.btnGetPIDClick(Sender: TObject);
var
  s: WideString;
  sPWD : WideString;
begin

    sPWD := nlock.ConvStringToDelimiteredString(edPassword.Text);
    s := nlock.GetPid(edVID.Text , sPWD);
    lbPID.Caption  := s;
    ShowErr();
end;

procedure TFrmNovinDelphiSample.btnGetSerialClick(Sender: TObject);
var s : WideString;
begin
    S := nlock.GetSerial();
    lbSerial.Caption  := S;
    ShowErr();

end;

procedure TFrmNovinDelphiSample.btnGetVersionClick(Sender: TObject);
var
    ver : WideString;
begin
    ver := nlock.GetVersion();
    lbVersion.Caption := ver;
    ShowErr();
end;




procedure TFrmNovinDelphiSample.Button1Click(Sender: TObject);
var
    memSize : String;
begin
    memSize := nlock.GetMemorySizeEx();
    lbMemorySize.Caption := memSize;
    ShowErr();
end;

procedure TFrmNovinDelphiSample.btnGetDataBlockClick(Sender: TObject);
var
    P : WideString;
    sPWD : WideString;
begin
    Screen.Cursor :=crHourGlass ;
    sPWD := nlock.ConvStringToDelimiteredString(edPassword.Text);
    P := nlock.GetDataBlockStr(edVID.Text  , sPWD , strTointDef(edAddress.Text,0 ), StrToIntDef(edLength.Text  ,0) );
    memDataBlock.Text := P;
    Screen.Cursor := crDefault ;
    ShowErr();

end;

procedure TFrmNovinDelphiSample.btnSetDataBlockClick(Sender: TObject);
var
    P : WideString;
    sPWD : WideString;
begin
    Screen.Cursor := crHourGlass ;
    sPWD := nlock.ConvStringToDelimiteredString(edPassword.Text);
    P := memDataBlock.Text ;
    nlock.SetDataBlockStr(edVID.Text  , sPWD , strTointDef(edAddress.Text,0 ), StrToIntDef(edLength.Text  ,0) , P);
    Screen.Cursor := crDefault ;
    ShowErr();

end;

procedure TFrmNovinDelphiSample.edAddressKeyPress(Sender: TObject;
  var Key: Char);
begin
if not (key  in ['0','1','2','3','4','5','6','7','8','9',char(13),char(8)] ) Then Key := char(0);
end;

procedure TFrmNovinDelphiSample.edHexChange(Sender: TObject);
var
  ascii : word;
begin
  if TEdit(Sender).Tag <> 0 Then Exit ;
  ascii := StrToIntDef(TEdit(Sender).Text , 0 );
  if ascii > 255 Then
  Begin
      TEdit(Sender).Tag := 1;
      TEdit(Sender).Text := '255';
      TEdit(Sender).Tag := 0;
      ascii := 255;
  End;
  edChar.Text := chr(ascii); 
end;

procedure TFrmNovinDelphiSample.edAddress2Change(Sender: TObject);
begin
  if TEdit(Sender).Tag <> 0 Then Exit ;
  if StrToIntDef(TEdit(Sender).Text , 0 ) > 400 Then
  Begin
      TEdit(Sender).Tag := 1;
      TEdit(Sender).Text := '400';
      TEdit(Sender).Tag := 0;
  End;


end;

procedure TFrmNovinDelphiSample.edCharChange(Sender: TObject);
begin
  try
    edHex.Text := inttoStr(ord(TEdit(Sender).text[1]));
  except;
    edhex.Text := '';
  end;

end;

procedure TFrmNovinDelphiSample.btnGetByteClick(Sender: TObject);
var
  Databyte : byte;
  sPWD : WideString;
begin
    sPWD := nlock.ConvStringToDelimiteredString(edPassword.Text);
    Databyte := nlock.GetDataByte(edVID.Text , sPWD , strtointdef(edAddress2.text , 0) );
    edHex.Text := inttostr(Databyte);
    ShowErr();


end;

procedure TFrmNovinDelphiSample.btnSetByteClick(Sender: TObject);
var
  Databyte : byte;
  sPWD : WideString;
begin
    DataByte := StrTointDef(edHex.Text,0)  ;
    sPWD := nlock.ConvStringToDelimiteredString(edPassword.Text);
    nlock.SetDataByte(edVID.Text , sPWD , strtointdef(edAddress2.text , 0) , Databyte );
    ShowErr();

end;

procedure TFrmNovinDelphiSample.chkCharViewClick(Sender: TObject);
var
  TempS : WideString;
begin
    Try
        medString.Tag := 1;
        lbResult.Tag := 1;

        if Not chkCharView.Checked Then
        Begin
            medString.Text := '';
            lbResult.Caption := '';

            medString.EditMask := '999.999.999.999.999.999.999.999.999.999.999.999.999.999.999.999;1; ';


            medString.Text := fStr ;
            lbResult.Caption := fResult ;
        END
        else
        BEGIN
            medString.EditMask := '';
            medString.Text := '';
            medString.MaxLength := 16;
            TempS := nlock.ConvDelimiteredStringToString(fStr);
            medString.Text := Temps;
            TempS := '';

            TempS := nlock.ConvDelimiteredStringToString(fResult);
            lbResult.Caption := TempS; 
        END;
    Finally
        medString.Tag := 0 ;
    end;

end;

procedure TFrmNovinDelphiSample.medStringChange(Sender: TObject);
var
  TempS : WideString;
  s1 : WideSTring;
begin
    if medString.Tag = 0 Then
    Begin
        s1 := medString.Text;
        if chkCharView.Checked Then
        Begin
            TempS := nlock.ConvStringToDelimiteredString(s1);
             fStr := Temps;
        end
        else
        Begin
            S1 := StringReplace(S1 , ' ', '' , [rfReplaceAll ]);
            fstr := S1;
        end;
    end;
end;

procedure TFrmNovinDelphiSample.FormCreate(Sender: TObject);
begin
  fstr := '0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0';
  fResult := '0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0';
  nlock.Init;
  ChangeDeviceEvent(MsgCDE);
end;

procedure TFrmNovinDelphiSample.medResultChange(Sender: TObject);
var
  TempS : WideString;
  s1 : WideSTring;
begin
    if lbResult.Tag = 0 Then
    Begin
        s1 := lbResult.Caption ;
        if chkCharView.Checked Then
        Begin
             TempS := nlock.ConvStringToDelimiteredString(S1);
             fResult := Temps;
        end
        else
        Begin
            S1 := StringReplace(S1 , ' ', '' , [rfReplaceAll ]);
            fResult  := S1;
        end;
    end;
end;

procedure TFrmNovinDelphiSample.btnEncryptClick(Sender: TObject);
Var
  sPWD : WideString;
begin
    fREsult := '';

    sPWD := nlock.ConvStringToDelimiteredString(edPassword.Text);
    fResult := nlock.GetEncryption(edVID.Text , sPWD , fStr , strTointDef(edIteration.Text , 0) );
    chkCharViewClick(chkCharView );

end;

procedure TFrmNovinDelphiSample.btnCopyClick(Sender: TObject);
begin
  fStr := fResult ;
  chkCharViewClick(chkCharView ); 
end;

procedure TFrmNovinDelphiSample.btnDecryptClick(Sender: TObject);
Var
  sPWD : WideString;
begin
    fREsult := '';
    sPWD := nlock.ConvStringToDelimiteredString(edPassword.Text);
    fResult := nlock.GetDecryption(edVID.Text , sPWD , fStr , strTointDef(edIteration.Text , 0));
    chkCharViewClick(chkCharView);

end;

procedure TFrmNovinDelphiSample.btnGetDeviceReadyClick(Sender: TObject);
begin
  ChangeDeviceEvent(MsgCDE);
end;

procedure TFrmNovinDelphiSample.cbxDevsChange(Sender: TObject);
begin
  nlock.SelectDevice(cbxDevs.Items[cbxDevs.itemindex]);
  nlock.GetDeviceReady();
  If nlock.ErrNo = 0 Then
  Begin
      lbChkDeviceReady.Caption := 'Ready' ;
      lbChkDeviceReady.Font.Color   := clGreen ;
  end
  Else
  Begin
      lbChkDeviceReady.Caption := 'Not Ready';
      lbChkDeviceReady.Font.Color := clRed;
  End;

end;

end.
